import http from '../../../class/Utils/Http.js'
import tips from '../../../class/Utils/Tips.js'
import user from '../../../class/Utils/User.js'
Page({
    data: {
        nodata:false,
        typemenuid: '1',
        liuyantag: false,
        list: [],
        param: {'type': '0', 'terms': '', 'qid': '', 'page': '1'},
        liuyan_list_state:false,
        xiaoxi_count:0
    },
    onLoad: function (p) {
        var that = this;
        var param = that.data.param
        param.type = p.type ? p.type : ''
        param.qid = p.qid ? p.qid : ''
        param.terms = p.terms ? p.terms : ''
        that.setData({
            param: param
        })
        console.log(param);
        http.req("/syq/trade_search", param, function (res) {
          
            //console.log(res.docs);
          if (res.docs.length>0){
            for (var i = 0; i < res.docs.length;i++){
              if(res.docs[i].type==2){
                res.docs[i].typecls = 'gongying'
              }
              if (res.docs[i].type == 1) {
                res.docs[i].typecls = 'qiugou'
              }
              if (res.docs[i].type == 3) {
                res.docs[i].typecls = 'zhaoshang'
              }
              if (res.docs[i].type == 4) {
                res.docs[i].typecls = 'shangfu'
              }
              res.docs[i].changduans = 1
              res.docs[i].shoucangs = 0
              res.docs[i].liuyan_state = 0;   // 留言是否展开
                res.docs[i].liuyan_gengduo = 0; //留言是否大于5条
                res.docs[i].liuyan_list  = [];  //已留言内容
                res.docs[i].liuyan_content = ''; //正在留言内容
                res.docs[i].liuyan_disabled = false; //正在按钮状态
            }
          }
            
            that.setData({
                list: res.docs
            })
            if(res.docs.length<1){
                that.setData({
                    nodata:true
                })
            }
        })
    },

  onShow: function () {
    var that = this
    var user = wx.getStorageSync('user')
    if (user) {
      if (user.log == 1) {
        /*
         获取未读消息数量
         */
        http.req('/Mingpian/xiaoxi', { uid: user.userid }, function (res) {
          console.log(res)
          that.setData({
            xiaoxi_count: res
          })
        })
      }
    }
  },
    //搜索跳转页面
    searchlink: function (e) {
        var that = this
        wx.redirectTo({
            url: '/pages/weishangji/search/search'
        })
    },
    //打开留言
    liuyanbtn: function (e) {
        var that = this
        var mid = e.currentTarget.dataset.id
        var index = e.currentTarget.dataset.index
        var list = that.data.list
        user.log_user(that, function () {
            list[index].liuyan_state = !list[index].liuyan_state
            that.setData({
                list: list
            })
            /*获取留言内容*/
            if(list[index].liuyan_state){
                var data = {
                    uid: wx.getStorageSync('user').userid,
                    mid: mid
                }
                http.reqload("/Api/liuyan_list", data, function (res) {
                    // console.log(res)
                    list[index].liuyan_list = res
                    if(list[index].liuyan_list.length >5){
                        list[index].liuyan_list.splice(5)
                        list[index].liuyan_gengduo = 1
                    }
                    that.setData({
                        list: list
                    })
                })
            }
            
        })

    },
    /*
     输入内容
     */
    content:function (e) {
        var that = this
        var index = e.currentTarget.dataset.index
        var list = that.data.list
        list[index].liuyan_content = e.detail.value
        this.setData({
            list:list
        })
    },
    liuyan:function (e) {
        var that = this
        var user = wx.getStorageSync("user")
        var mid = e.currentTarget.dataset.id
        var index = e.currentTarget.dataset.index
        var list = that.data.list
        if(list[index].liuyan_content==''){
            tips.error('请输入留言内容')
            return
        }
        list[index].liuyan_disabled = true
        that.setData({
            list:list
        })
        var data = {
            uid:user.userid,
            content:list[index].liuyan_content,
            mid:mid
        }


        http.reqload("/Api/add_liuyan",data,function (res) {
            if(res.code=='202'){
                console.log(res);
                tips.ok(res.msg_val)
                list[index].liuyan_list.unshift({'poster':res.poster,'content':res.content})
                list[index].liuyan_disabled = false
                list[index].liuyan_content = ''
                if(list[index].liuyan_list.length>5){
                    list[index].liuyan_list.splice(5)
                    list[index].liuyan_gengduo = 1
                }
                that.setData({
                    list:list
                })
            }else{
                tips.error(res.msg_val)
            }
        })
        console.log(list)
    },
    /*
    跳转发布
     */
    fabu:function () {
      var that = this
        user.log_user(that,function () {
            tips.goto('../add/add')
        })
    },
    /*
     点击名片
     */
    mingpian:function (e) {
        var that = this
        user.log_user(this, function (res) {
            var poster = e.currentTarget.dataset.poster
            var cid = e.currentTarget.dataset.cid
            if(cid){
                var dd ={
                    poster:poster,
                    cid:cid
                }
                console.log(dd)
                http.reqload("/Api/mingpian",dd,function (res) {
                    console.log(res)
                    if(res.code == 200){
                        tips.goto('/pages/mingpian/card/card?id='+res.mid)
                    }else{
                        tips.error(res.msg_val)
                    }
                })
            }else{
                tips.error("暂无名片")
            }

        })
    },
    /*
     收藏
     */
    shoucang: function (e) {
        var that = this
        /*
         log_user 返回  缓存user
         */
        user.log_user(this, function (res) {
            var index = e.currentTarget.dataset.index
            var id = e.currentTarget.dataset.id
            var list = that.data.list
            var scyufou = list[index].shoucangs
            var data = {
              uid: res.userid,
              tid: id
            }
            http.req("/Api/add_shoucang", data, function (res) {
                if (res.code == '203') {
                   tips.ok(res.msg_val);
                  list[index].shoucangs = 1
                  that.setData({
                    list:list
                  })



                } else {
                    tips.error(res.msg_val)
                }
            })
        })

    },
    onReachBottom: function () {
        var that = this
        wx.showLoading({
            title: '加载中',
        })
        var param = that.data.param
        param.page++
        that.setData({
            param: param
        })

        var list = that.data.list
        //that.nav(param);

        http.req("/syq/trade_search", param, function (res) {
            
            for (var i = 0; i < res.docs.length; i++) {
              if (res.docs[i].type == 2) {
                res.docs[i].typecls = 'gongying'
              }
              if (res.docs[i].type == 1) {
                res.docs[i].typecls = 'qiugou'
              }
              if (res.docs[i].type == 3) {
                res.docs[i].typecls = 'zhaoshang'
              }
              if (res.docs[i].type == 4) {
                res.docs[i].typecls = 'shangfu'
              }
              res.docs[i].changduans = 1
              res.docs[i].shoucangs = 0
                res.docs[i].liuyan_state = 0;   // 留言是否展开
                res.docs[i].liuyan_gengduo = 0; //留言是否大于5条
                res.docs[i].liuyan_list  = [];  //已留言内容
                res.docs[i].liuyan_content = ''; //正在留言内容
                res.docs[i].liuyan_disabled = false; //正在按钮状态
              list.push(res.docs[i])
            }
            that.setData({
                list: list
            })
            wx.hideLoading()
        })
    },
    /*
     授权登录
     */
    bindGetUserInfo: function (e) {
        var that = this
        user.user(this, function (res) {
            that.setData({
                shouquan: 0,
                userinfo: res

            })
            if (res.log == 0) {
                that.setData({
                    bangdingtoocletag: true
                })
            }
        })

    },
    /*
     弹出 登录
     */
    bangdingtoocle: function () {
        var that = this
        that.setData({
            bangdingtoocletag: !that.data.bangdingtoocletag
        })
    },
    /*
     点击登录
     */
    denglu: function (e) {
        var that = this
        var zhanghao = e.detail.value.zhanghao
        var mima = e.detail.value.mima
        if (!zhanghao) {
            tips.error("账号不能为空");
            return false
        }
        if (!mima) {
            tips.error("密码不能为空");
        }
        http.req("/Api/log", {login: zhanghao, passwd: mima, uid: wx.getStorageSync('user').userid}, function (res) {
            console.log(res)
            if (res.code == '200') {
                var user = wx.getStorageSync('user')
                user.log = 1
                wx.setStorageSync('user', user)
                tips.ok('登录成功')
                that.setData({
                    bangdingtoocletag: false,
                    shengyihao: zhanghao
                })
            } else {
                tips.error(res.msg_val)
            }
        })
    },
  //长短 介绍切换
  changeintro: function (e) {
    var that = this
    console.log(e)
    var presentindex = e.currentTarget.dataset.index
    var datalist = that.data.list
    var datalistzhi = datalist[presentindex].changduans

    for (var i = 0; i < datalist.length; i++) {

      if (datalistzhi == 1) {
        datalist[presentindex].changduans = 0

      }
      else {
        datalist[presentindex].changduans = 1
      }
    }


    that.setData({
      list: datalist
    })
    console.log(datalist)


  },
  //点击图片放大
  previewImage: function (e) {
    var that = this;
    console.log(e)


    //获取点击的数据里的图片

    var thisindex = e.currentTarget.dataset.index
    var thatdatalist = that.data.list
    var thatdatalistindex = thatdatalist[thisindex].pic_list_img

    var newimgdata = [];
    for (var i = 0; i < thatdatalistindex.length; i++) {
      if (thatdatalistindex[i]) {
        newimgdata.push('http://img.i.album.toocle.com/0-0-0/' + thatdatalistindex[i]);
      }

    }
    that.setData({
      newimgdata: newimgdata,
    });

    console.log(newimgdata)







    var current = e.currentTarget.dataset.src;
    wx.previewImage({
      current: current, // 当前显示图片的http链接  
      urls: that.data.newimgdata // 需要预览的图片http链接列表  
    })
  },
  //获取绑定微信的手机号码
  getPhoneNumber: function (e) {
    var that = this
    user.getPhoneNumber(e, that)
  },
})
